<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="block review-add">
    <div class="block-title"><strong><?php echo __('Write Your Own Review') ?></strong></div>
<div class="block-content">
<?php if ($this->getAllowWriteReviewFlag()): ?>
<form action="<?php echo $this->getAction() ?>" class="review-form" method="post" id="review-form">
    <?php echo $this->getBlockHtml('formkey'); ?>
    <?php echo $this->getChildHtml('form_fields_before')?>
    <fieldset class="fieldset review-fieldset" data-hasrequired="<?php __('* Required Fields'); ?>">
        <legend class="legend review-legend"><span><?php echo __("You're reviewing:"); ?></span><strong><?php echo $this->escapeHtml($this->getProductInfo()->getName()) ?></strong></legend><br />
        <?php if ($this->getRatings() && $this->getRatings()->getSize()): ?>
        <span id="input-message-box"></span>
        <fieldset class="field required review-field-ratings">
            <legend class="label"><span><?php echo __('How do you rate this product?') ?><span></legend><br/>
            <div class="control">
                <div class="nested" id="product-review-table">
                    <?php foreach ($this->getRatings() as $_rating): ?>
                        <div class="field choice review-field-rating">
                            <label class="label" id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_rating_label"><span><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></span></label>
                            <div class="control review-control-vote">
                            <?php $options = $_rating->getOptions();?>
                            <?php $iterator = 1; foreach ($options as $_option): ?>
                                <input
                                    type="radio"
                                    name="ratings[<?php echo $_rating->getId() ?>]"
                                    id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>"
                                    value="<?php echo $_option->getId() ?>"
                                    class="radio"
                                    data-validate="{required:true, messages:{required:'Please select one of each of the ratings above.'}}"
                                    aria-labelledby="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_rating_label <?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>_label" />
                                <label
                                    class="rating-<?php echo $iterator; ?>"
                                    for="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>"
                                    title="<?php echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?>"
                                    id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>_label">
                                    <span><?php echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?></span>
                                </label>
                            <?php $iterator++; ?>
                            <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <input type="hidden" name="validate_rating" class="validate-rating" value="" />
            </div>
        </fieldset>
    <?php endif ?>
        <div class="field review-field-nickname required">
            <label for="nickname_field" class="label"><span><?php echo __('Nickname') ?></span></label>
            <div class="control">
                <input type="text" name="nickname" id="nickname_field" class="input-text" data-validate="{required:true}" value="<?php echo $this->escapeHtml($data->getNickname()) ?>" />
            </div>
        </div>
        <div class="field review-field-summary required">
            <label for="summary_field" class="label"><span><?php echo __('Summary of Your Review') ?></span></label>
            <div class="control">
                <input type="text" name="title" id="summary_field" class="input-text" data-validate="{required:true}" value="<?php echo $this->escapeHtml($data->getTitle()) ?>" />
            </div>
        </div>
        <div class="field review-field-text required">
            <label for="review_field" class="label"><span><?php echo __('Review') ?></span></label>
            <div class="control">
                <textarea name="detail" id="review_field" cols="5" rows="3" data-validate="{required:true}"><?php echo $this->escapeHtml($data->getDetail()) ?></textarea>
            </div>
        </div>
    </fieldset>
    <div class="actions-toolbar review-form-actions">
        <div class="primary actions-primary">
            <button type="submit" class="action submit primary"><span><?php echo __('Submit Review') ?></span></button>
        </div>
        <!-- <button type="button" class="action cancel"><span><?php echo __('Cancel') ?></span></button> -->
    </div>
</form>
<script>
require([
    "jquery",
    "mage/mage"
], function($){

    $('#review-form').mage('validation', {
        errorPlacement: function (error, element) {
            if (element.parents('#product-review-table').length) {
                $('#product-review-table').siblings(this.errorElement + '.' + this.errorClass).remove();
                $('#product-review-table').after(error);
            } else {
                element.after(error);
            }
        }
    });

});
</script>
<?php else: ?>
    <div class="message info notlogged" id="review-form">
        <div>
            <?php echo __('Only registered users can write reviews. Please, <a href="%1">log in</a> or <a href="%2">register</a>', $this->getLoginLink(), $this->helper('Magento\Customer\Helper\Data')->getRegisterUrl()) ?>
        </div>
    </div>
<?php endif ?>
</div>
</div>
